package com.xdhl.fu.gray.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.utils.StringUtils;
import com.xdhl.fu.common.core.web.controller.BaseController;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.core.web.page.TableDataInfo;
import com.xdhl.fu.common.log.annotation.Log;
import com.xdhl.fu.common.log.enums.BusinessType;
import com.xdhl.fu.common.security.annotation.RequiresPermissions;
import com.xdhl.fu.common.security.utils.SecurityUtils;
import com.xdhl.fu.gray.domain.GrayService;
import com.xdhl.fu.gray.service.IGrayServiceService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 灰度服务控制器
 *
 * @author 朱力维
 * @time 2022/6/6 10:17
 */
@RestController
@RequestMapping("/service")
public class GrayServiceController extends BaseController {

    @Resource
    private IGrayServiceService grayServiceService;

    /**
     * 查询服务列表
     */
    @RequiresPermissions("gray:service:list")
    @GetMapping("/list")
    public TableDataInfo list(GrayService grayService) {
        startPage();

        LambdaQueryWrapper<GrayService> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(grayService.getEnvCode()), GrayService::getEnvCode, grayService.getEnvCode());

        List<GrayService> list = grayServiceService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取服务详细信息
     */
    @RequiresPermissions("gray:service:query")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") String dataId) {
        return AjaxResult.success(grayServiceService.getById(dataId));
    }

    /**
     * 新增服务
     */
    @RequiresPermissions("gray:service:add")
    @Log(title = "服务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GrayService grayService) {
        grayService.setCreateBy(SecurityUtils.getUsername());
        grayService.setUpdateBy(SecurityUtils.getUsername());
        return AjaxResult.success(grayServiceService.save(grayService));
    }

    /**
     * 修改服务
     */
    @RequiresPermissions("gray:service:edit")
    @Log(title = "服务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GrayService grayService) {
        return AjaxResult.success(grayServiceService.updateById(grayService));
    }

    /**
     * 删除服务
     */
    @RequiresPermissions("gray:service:remove")
    @Log(title = "服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable String[] dataIds) {
        return AjaxResult.success(grayServiceService.removeByIds(Arrays.asList(dataIds)));
    }

}
